package com.gateway.bootstrap.convert;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gateway.common.dto.ApiDefinition;
import com.gateway.common.dto.sync.ApiSyncData;
import com.gateway.common.enums.ApiStatusEnum;
import com.gateway.common.enums.AuthenticationTypeEnum;
import com.gateway.common.enums.AuthorizationTypeEnum;
import com.gateway.common.enums.YesOrNoEnum;
import com.gateway.plugin.base.convert.ApiDataConvert;
import com.gateway.repository.bean.GatewayApi;
import com.gateway.repository.service.GatewayApiService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/6 10:32
 **/
@Component
public class ApiDataConvertFunction implements ApiDataConvert {
    private static final Logger log = LoggerFactory.getLogger(ApiDataConvertFunction.class);
    @Resource
    private GatewayApiService gatewayApiService;

    @Override
    public ApiDefinition apply(ApiSyncData apiSyncData) {
        GatewayApi gatewayApi = gatewayApiService.getOne(
                new QueryWrapper<GatewayApi>().lambda()
                        .select(
                                GatewayApi::getApiCode,
                                GatewayApi::getAuthentication,
                                GatewayApi::getAuthenticationType,
                                GatewayApi::getAuthorization,
                                GatewayApi::getAuthorizationType,
                                GatewayApi::getApiPath,
                                GatewayApi::getRpcType,
                                GatewayApi::getCsrf,
                                GatewayApi::getModule,
                                GatewayApi::getOpen,
                                GatewayApi::getStatus,
                                GatewayApi::getEnv
                        )
                        .eq(GatewayApi::getEnv, apiSyncData.getEnv())
                        .eq(GatewayApi::getApiCode, apiSyncData.getApiCode())
        );
        if (Objects.isNull(gatewayApi)) {
            log.error("ApiDataConvertFunction query Gateway Api empty, syncData={}", apiSyncData);
            return null;
        }
        return convert(gatewayApi);
    }

    public static ApiDefinition convert(GatewayApi gatewayApi) {
        return ApiDefinition.Builder.builder()
                .apiCode(gatewayApi.getApiCode())
                .authentication(gatewayApi.getAuthentication())
                .authorization(gatewayApi.getAuthorization())
                .path(gatewayApi.getApiPath())
                .rpcType(gatewayApi.getRpcType())
                .csrf(gatewayApi.getCsrf())
                .module(gatewayApi.getModule())
                .open(gatewayApi.getOpen())
                .status(ApiStatusEnum.valueOf(gatewayApi.getStatus()))
                .env(gatewayApi.getEnv())
                .authenticationType(AuthenticationTypeEnum.getByType(gatewayApi.getAuthenticationType()))
                .authorizationType(AuthorizationTypeEnum.getByType(gatewayApi.getAuthorizationType()))
                .build();
    }
}
